package com.moxing.admin.common.utils;


public class DesensitizationUtil {

    /**
     * 证件号码脱敏：保留前后各两位，其他位用*号屏蔽，长度保持不变
     * 如：142223199009191112 -> 14**************12
     * G12345678 -> G1*****78
     */
    public static String idCardDesensitize(String idCard) {
        if (idCard == null || idCard.length() <= 4) {
            return idCard;
        }

        int length = idCard.length();
        String firstPart = idCard.substring(0, 2);
        String lastPart = idCard.substring(length - 2);
        StringBuilder middlePart = new StringBuilder();

        for (int i = 0; i < length - 4; i++) {
            middlePart.append('*');
        }

        return firstPart + middlePart + lastPart;
    }

    /**
     * 手机号码脱敏：屏蔽中间四位数
     * 如：15918885589 -> 159****5589
     */
    public static String phoneDesensitize(String phone) {
        if (phone == null || phone.length() < 7) {
            return phone;
        }

        String firstPart = phone.substring(0, 3);
        String lastPart = phone.substring(phone.length() - 4);

        return firstPart + "****" + lastPart;
    }

    /**
     * 固话脱敏：屏蔽中间四位
     * 如：02086112345 -> 020****2345
     */
    public static String telephoneDesensitize(String telephone) {
        if (telephone == null || telephone.length() < 5) {
            return telephone;
        }

        // 区号可能是3-4位，这里假设区号是3位
        if (telephone.length() >= 8) {
            String areaCode = telephone.substring(0, 3);
            String lastPart = telephone.substring(telephone.length() - 4);
            return areaCode + "****" + lastPart;
        } else {
            // 对于长度不够的电话，只保留前后各两位
            return telephone.substring(0, 2) + "****" + telephone.substring(telephone.length() - 2);
        }
    }

    /**
     * 邮箱地址脱敏
     * 账号名大于4字符的邮箱账号名屏蔽后四位数
     * 账号名小于或等于4字符，则屏蔽现有字符总数的1/2
     */
    public static String emailDesensitize(String email) {
        if (email == null || !email.contains("@")) {
            return email;
        }

        String[] parts = email.split("@");
        if (parts.length != 2) {
            return email;
        }

        String username = parts[0];
        String domain = parts[1];

        if (username.length() > 4) {
            String visiblePart = username.substring(0, username.length() - 4);
            return visiblePart + "****@" + domain;
        } else {
            int maskCount = (int) Math.ceil(username.length() / 2.0);
            String visiblePart = username.substring(0, username.length() - maskCount);
            StringBuilder mask = new StringBuilder();
            for (int i = 0; i < maskCount; i++) {
                mask.append('*');
            }
            return visiblePart + mask + "@" + domain;
        }
    }

    /**
     * 银行卡号脱敏：保留前后各四位，其他位用*号屏蔽
     * 如：6214***********3786
     */
    public static String bankCardDesensitize(String bankCard) {
        if (bankCard == null || bankCard.length() <= 8) {
            return bankCard;
        }

        String firstPart = bankCard.substring(0, 4);
        String lastPart = bankCard.substring(bankCard.length() - 4);
        StringBuilder middlePart = new StringBuilder();

        for (int i = 0; i < bankCard.length() - 8; i++) {
            middlePart.append('*');
        }

        return firstPart + middlePart + lastPart;
    }

    /**
     * 地址脱敏：屏蔽地址中前1/2字符
     * 如："浙江省温州市北海区双堡西路87-12号" -> "*********区双堡西路87-12号"
     */
    public static String addressDesensitize(String address) {
        if (address == null || address.length() <= 1) {
            return address;
        }

        int maskLength = address.length() / 2;
        StringBuilder mask = new StringBuilder();
        for (int i = 0; i < maskLength; i++) {
            mask.append('*');
        }

        String visiblePart = address.substring(maskLength);
        return mask + visiblePart;
    }

    /**
     * 旅客姓名脱敏
     * 两字姓名：如张三 -> 张*, ZHANG/SAN -> ZHANG/***
     * 三字姓名：如张三四 -> 张*四, ZHANG/SANSI -> ZHANG/***SI
     * 四个字及以上：屏蔽最中间两个字，如张三四五六 -> 张**五六, ZHANG/SANSIWULIU -> ZHANG/*****WULIU
     */
    public static String nameDesensitize(String name) {
        if (name == null || name.isEmpty()) {
            return name;
        }

        // 检查是否是英文姓名（包含斜杠）
        if (name.contains("/")) {
            String[] parts = name.split("/");
            if (parts.length != 2) {
                return name;
            }

            String firstName = parts[0];
            String lastName = parts[1];

            if (lastName.length() <= 2) {
                return firstName + "/***";
            } else if (lastName.length() == 3) {
                return firstName + "/***" + lastName.substring(2);
            } else {
                int maskStart = (lastName.length() - 2) / 2;
                String firstPart = lastName.substring(0, maskStart);
                String lastPart = lastName.substring(maskStart + 2);
                StringBuilder mask = new StringBuilder();
                for (int i = 0; i < lastName.length() - firstPart.length() - lastPart.length(); i++) {
                    mask.append('*');
                }
                return firstName + "/" + firstPart + mask + lastPart;
            }
        } else {
            // 中文姓名处理
            if (name.length() == 2) {
                return name.charAt(0) + "*";
            } else if (name.length() == 3) {
                return name.charAt(0) + "*" + name.charAt(2);
            } else if (name.length() >= 4) {
                int maskStart = (name.length() - 2) / 2;
                String firstPart = name.substring(0, maskStart);
                String lastPart = name.substring(maskStart + 2);
                StringBuilder mask = new StringBuilder();
                for (int i = 0; i < 2; i++) {
                    mask.append('*');
                }
                return firstPart + mask + lastPart;
            } else {
                return name;
            }
        }
    }

    /**
     * 票号脱敏：屏蔽中间五位数
     * 如：7841234567890 -> 7841*****7890
     */
    public static String ticketDesensitize(String ticket) {
        if (ticket == null || ticket.length() < 6) {
            return ticket;
        }

        String firstPart = ticket.substring(0, 4);
        String lastPart = ticket.substring(ticket.length() - 4);

        return firstPart + "*****" + lastPart;
    }

    /**
     * 出生日期脱敏：屏蔽月日
     * 如：2020.05.06 -> 2020.**.**
     */
    public static String birthDateDesensitize(String birthDate) {
        if (birthDate == null) {
            return null;
        }

        // 支持多种日期格式
        if (birthDate.matches("\\d{4}\\.\\d{2}\\.\\d{2}")) {
            return birthDate.substring(0, 5) + "**.**";
        } else if (birthDate.matches("\\d{4}-\\d{2}-\\d{2}")) {
            return birthDate.substring(0, 5) + "**-**";
        } else if (birthDate.matches("\\d{4}/\\d{2}/\\d{2}")) {
            return birthDate.substring(0, 5) + "**/**";
        } else if (birthDate.matches("\\d{8}")) {
            return birthDate.substring(0, 4) + "****";
        } else {
            return birthDate;
        }
    }

    /**
     * 工作单位脱敏：屏蔽单位中前1/2字符
     * 如：广东南航电子商务有限公司(客户服务部)业务管理部 -> ********司(客户服务部)业务管理部
     */
    public static String companyDesensitize(String company) {
        return addressDesensitize(company); // 同地址脱敏逻辑
    }

    /**
     * 职务脱敏：屏蔽单位中前1/2字符
     * 如：业务支持 -> **支持
     */
    public static String jobTitleDesensitize(String jobTitle) {
        return addressDesensitize(jobTitle); // 同地址脱敏逻辑
    }

    /**
     * 会员卡号脱敏：屏蔽中间四位数
     * 如：180018001800 -> 1800****1800
     */
    public static String membershipCardDesensitize(String cardNo) {
        if (cardNo == null || cardNo.length() < 8) {
            return cardNo;
        }

        String firstPart = cardNo.substring(0, 4);
        String lastPart = cardNo.substring(cardNo.length() - 4);

        return firstPart + "****" + lastPart;
    }

    // 测试方法
    public static void main(String[] args) {
        // 测试各种脱敏功能
        System.out.println("证件号码: " + idCardDesensitize("142223199009191112"));
        System.out.println("证件号码: " + idCardDesensitize("G12345678"));
        System.out.println("手机号码: " + phoneDesensitize("15918885589"));
        System.out.println("固话: " + telephoneDesensitize("02086112345"));
        System.out.println("邮箱: " + emailDesensitize("feixiang@136.com"));
        System.out.println("邮箱: " + emailDesensitize("1234@163.com"));
        System.out.println("邮箱: " + emailDesensitize("123@163.com"));
        System.out.println("银行卡: " + bankCardDesensitize("62148888777766663786"));
        System.out.println("地址: " + addressDesensitize("浙江省温州市北海区双堡西路87-12号"));
        System.out.println("姓名: " + nameDesensitize("张三"));
        System.out.println("姓名: " + nameDesensitize("张三四"));
        System.out.println("姓名: " + nameDesensitize("张三四五六"));
        System.out.println("姓名: " + nameDesensitize("ZHANG/SAN"));
        System.out.println("姓名: " + nameDesensitize("ZHANG/SANSI"));
        System.out.println("姓名: " + nameDesensitize("ZHANG/SANSIWULIU"));
        System.out.println("票号: " + ticketDesensitize("7841234567890"));
        System.out.println("出生日期: " + birthDateDesensitize("2020.05.06"));
        System.out.println("工作单位: " + companyDesensitize("广东南航电子商务有限公司(客户服务部)业务管理部"));
        System.out.println("职务: " + jobTitleDesensitize("业务支持"));
        System.out.println("会员卡号: " + membershipCardDesensitize("180018001800"));
    }
}